<template>
  <div class="app-container">
    <el-row :gutter="12" v-for="(row, index) in sliceList(supplierList, 6)" :key="index" style="margin-bottom: 20px;">
      <el-col :span="3" v-for="(item, i) in row" :key="i">
        <el-card shadow="hover" @click.native="selectSupplier(item)" style="cursor: pointer;" v-bind:class="{'class1': item.cloudSupplier==supplierSelected.cloudSupplier}">
          <span v-if="item">{{item.cloudSupplierName}}</span>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
            <el-input
                v-model="deptName"
                placeholder="请输入分类名称"
                clearable
                size="small"
                prefix-icon="el-icon-search"
                style="margin-bottom: 20px"
            />
        </div>
        <div class="head-container leftTree">
          <el-tree
            :data="deptPanel.deptOptions"
            :props="deptPanel.defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            default-expand-all
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>

      <el-col :span="20" :xs="24">
        <el-row :gutter="10" class="mb8">
          <el-form :model="queryParams" ref="queryForm" :inline="true">
            <el-form-item label="规格名称">
              <el-input
                v-model="queryParams.searchValue"
                placeholder="请输入规格名称"
                clearable
                size="small"
                style="margin-bottom: 20px"
                @keyup.enter.native="getList"
              />
            </el-form-item>
            <el-form-item>
              <el-button
                type="primary"
                icon="el-icon-search"
                size="small"
                @click="getList"
              >搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="small"
                @click="handleAdd" plain
              >新增</el-button>
            </el-form-item>
          </el-form>
          <el-table v-loading="loading" :data="dataList">
            <el-table-column
              label="规格名称"
              prop="specName"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="云服务商"
              prop="supplier"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="描述"
              prop="remark"
              :show-overflow-tooltip="true"
            />
            <el-table-column
              label="操作"
              width="100"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  v-if="scope.row.userId !== 1"
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                >修改</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.row)"
                >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-row>
      </el-col>
    </el-row>

    <!-- 新增云服务商 -->
    <el-dialog :title="dialog.title" :visible.sync="dialog.open" width="50%" append-to-body>
      <el-form ref="formData" :model="formData" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="规格名称" prop="specName">
              <el-input v-model="formData.specName" placeholder="请输入规格名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别" prop="categoryCode">
              <treeselect v-model="formData.categoryCode" :options="deptPanel.deptOptions" :show-count="true" :normalizer="normalizer" placeholder="请选择类别" >
              </treeselect>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="云服务商" prop="supplier">
              <el-input v-model="formData.supplier" placeholder="请输入云服务商" readonly />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否信创" prop="isXc">
              <el-switch
                v-model="formData.isXc"
                active-color="#13ce66"
                inactive-color="#ff4949">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="cpu" prop="cpuNumber">
              <el-select v-model="formData.cpuNumber" placeholder="请选择">
                <el-option
                  v-for="item in cpuOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="内存" prop="memorySize">
              <el-select v-model="formData.memorySize" placeholder="请选择">
                <el-option
                  v-for="item in memoryOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="formData.remark" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 认</el-button>
        <el-button @click="dialog.open=false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { DelectDetail, DetailId, DetailList, specTreeselect } from '@/api/basic/specifica'
  import { logicList as supplierList, saveSpecGroup } from '@/api/basic/services'
  import Treeselect from '@riophae/vue-treeselect'
  import '@riophae/vue-treeselect/dist/vue-treeselect.css'

  export default {
  name: "SpecDetail",
  components: { Treeselect },
  data() {
    return {
      loading: true,
      total: 0,
      dataList: [],//查询的数据
      formData: {//提交的表单
        sort : 1
      },
      queryParams: {//查询参数
        pageNum: 1,
        pageSize: 10,
        supplier: null,
        category: null
      },
      dialog: {//新增、修改弹窗
        title: "",
        open: false,
      },
      supplierList: [],//云服务商列表
      supplierSelected: null,//选中的云服务商
      deptName: "",
      deptPanel: {
        deptName: "",
        deptOptions: undefined,
        defaultProps: {
          children: "children",
          label: "label",
        },
      },
      rules: {
        specName: [
          {required: true, message: "请输入规格名", trigger: "blur"},
        ],
        categoryCode: [
          {required: true, message: "请选择规格分类", trigger: "change"},
        ],
        supplier: [
          {required: true, message: "必须选择云服务商", trigger: "blur"},
        ],
        cpuNumber: [
          {required: true, message: "请输入cpu", trigger: "blur"},
        ],
        memorySize: [
          {required: true, message: "请输入内存", trigger: "blur"},
        ],
      },
      cpuOptions: [
        {value: 1, label: '1核'},{value: 2, label: '2核'},{value: 4, label: '4核'},{value: 8, label: '8核'},{value: 16, label: '16核'},{value: 32, label: '32核'},
      ],
      memoryOptions: [
        {value: 2, label: '2G'},{value: 4, label: '4G'},{value: 8, label: '8G'},{value: 16, label: '16G'},{value: 32, label: '32G'},{value: 64, label: '64G'}
      ]
    };
  },
  watch: {
    // 根据名称筛选部门树
    deptName(val) {
        this.$refs.tree.filter(val);
    },
  },
  computed: {
    sliceList() {
      return function (data,count) {
        if (data != undefined) {
          let index = 0;
          let arrTemp = [];
          for (let i = 0; i < data.length; i++) {
            index = parseInt(i / count);
            if (arrTemp.length <= index) {
              arrTemp.push([]);
            }
            arrTemp[index].push(data[i])
          }
          return arrTemp
        }
      }
    }
  },
  created() {
    this.init();
  },
  methods: {
    resetQuery(){
      this.queryParams.searchValue=''
      this.queryParams.pageNum=1
      this.getList()
    },
    init(){
      supplierList().then(rep=>{
        if(rep.rows){
          this.supplierList = rep.rows;
          this.supplierSelected = rep.rows[0];//默认选中第一个云服务商
        }
        return null;
      }).then(() => {
        this.getTreeselect();//初始化部门树
        this.getList();//查询数据
      });

    },
    getList() {
      this.loading = true;
      if(!this.supplierSelected){
        this.$message.warning("请选择云服务商");
        this.loading = false;
        return;
      }
      this.queryParams.supplier = this.supplierSelected.cloudSupplier;
      DetailList(this.queryParams).then((response) => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    selectSupplier(supplier){
      this.supplierSelected = supplier;
      this.getList();
    },
    /** 查询部门下拉树结构 */
    getTreeselect() {
      specTreeselect().then((response) => {
        this.deptPanel.deptOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.category = data.code;
      this.deptPanel.selectedCode = data.code;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dialog.open = true;
      this.dialog.title = "新增规格";
      this.formData = {
        supplier: this.supplierSelected.cloudSupplier,
        categoryCode: this.deptPanel.selectedCode,
        cpuNumber: 1,
        memorySize: 2
      }
    },
    submitForm(){
      //解析cpu和内存
      let cpuKey = this.formData.isXc? "1hvCpu": "vcpu", memoryKey = this.formData.isXc? "1Gnc": "memory";
      this.formData.details = new Object();
      this.formData.details[cpuKey] = {
          supplier: this.supplierSelected.cloudSupplier,
          isXc: this.formData.isXc,
          resourceCode: cpuKey,
          resourceName: this.formData.isXc? "信创CPU": "CPU",
          resourceCount: this.formData.cpuNumber
        };
      this.formData.details[memoryKey] = {
          supplier: this.supplierSelected.cloudSupplier,
          isXc: this.formData.isXc,
          resourceCode: memoryKey,
          resourceName: this.formData.isXc? "信创内存": "内存",
          resourceCount: this.formData.memorySize
        };
      saveSpecGroup(this.formData).then(rep => {
        if(rep.success){
          this.dialog.open = false;
          this.getList();
        }else{
          this.$message.error(rep.msg);
        }
      })
    },
    normalizer(node) {
      return {
        id: node.code,
        label: node.label,
        children: node.children,
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.dialog.open = true;
      this.dialog.title = "新增规格";
      DetailId(row.id).then((res) => {
        this.formData = res.data;
        //根据cpuKey判断是否信创
        this.formData.isXc = this.formData.cpuCode!='vcpu';//cpu编号不是vcpu的全是信创
      });
    },
    handleDelete(row) {
      this.$confirm(`是否确认删除？`, "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(function () {
        return DelectDetail(row.id);
      }).then(() => {
        this.$message.success("删除成功");
        this.getList();
      });
    },
  },
};
</script>
<style scoped lang="scss">
.app-container {
    .leftTree {
        height: 80vh;
        overflow-y: auto;
    }
}
.class1{
  background: #409EFF;
  color: #eee;
}
</style>
